package br.com.gkarian.intercepts;

import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.com.caelum.vraptor.resource.ResourceMethod;
import br.com.gkarian.anotacoes.RestritoAdmin;
import br.com.gkarian.controller.MainController;
import br.com.gkarian.web.UsuarioWeb;

@RequestScoped
@Intercepts
public class AutenticacaoInterceptor implements Interceptor {
	
	private final UsuarioWeb usuario;
	private final Result result;

	public AutenticacaoInterceptor(UsuarioWeb usuario, Result result) {
		this.usuario = usuario;
		this.result = result;
	}

	public boolean accepts(ResourceMethod method) {
		if (method.containsAnnotation(RestritoAdmin.class)) {
			if (usuario.isLogado()) {
				if (usuario.getAuth() == 7) {
					return false;
				}
				return true;
			}
			return true;
		} else {
			return false;
		}
	}

	public void intercept(InterceptorStack stack, ResourceMethod method,
			Object resourceInstance) throws InterceptionException {
 		result.redirectTo(MainController.class).index();
	}
}